﻿using GsOrganization.Common.Extend;
using GsOrganization.Common.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GsOrganization.TcpPacket
{
    /// <summary>
    /// 服务器消息包
    /// </summary>
    public class ServerMessagePacket: IPacket
    {
        public IHeader Header { get; set; }
        public string PayLoad { get; set; }

        public ServerMessagePacket(ushort CommandId, string PayLoad)
        {
            this.Header = new Header()
            {
                CommandId = CommandId,

            };
            this.PayLoad = PayLoad;
        }

        public byte[] ToBytes()
        {
            byte[] bsPayLoad = PayLoad.UTF8BytesFromString().AES_Encrypt();
            Header.MessageLength = (ushort)(bsPayLoad.Length + IHeader.HeaderLength);
            Header.TimeStamp = DateTimeExtend.GetTimeStampS();
            byte[] bsPacket = new byte[Header.MessageLength];
            Header.ToBytes().CopyTo(bsPacket, 0);
            bsPayLoad.CopyTo(bsPacket, IHeader.HeaderLength);
            return bsPacket;
        }
    }
}
